local ma__lingdao = fk.CreateSkill {
  name = "ma__lingdao",
}

Fk:loadTranslationTable {
  ["ma__lingdao"] = "领道",
  [":ma__lingdao"] = "每回合每项限一次，你使用牌后，可以用所有手牌从以下一项交换至多X张牌（X为你的牌数，且至多为5）：1.牌堆顶；2.本回合的弃牌堆；3.一名其他角色的手牌。",

  ["ma__lingdao1"] = "牌堆顶",
  ["ma__lingdao2"] = "本回合的弃牌堆",
  ["ma__lingdao3"] = "一名其他角色的手牌",
  ["#ma__lingdao-choosing"] = "领道：用所有手牌从一项处交换至多%arg张牌",
  ["#ma__lingdao-exchange"] = "领道: 用所有你的手牌交换牌堆顶的至多%arg张牌",
  ["#ma__lingdao-discard_pile"] = "领道: 用所有你的手牌交换本回合弃牌堆中的至多%arg张牌",
  ["#ma__lingdao-choose"] = "领道: 用所有你的手牌交换一名其他角色至多%arg张手牌",

  ["$ma__lingdao1"] = "救赎之道，就在手中。",
  ["$ma__lingdao2"] = "天地皆病，我以符水医其不仁。",
}

ma__lingdao:addEffect(fk.CardUseFinished, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(ma__lingdao.name) and #player:getCardIds("he") > 0 then
      local room = player.room
      local choices = {}
      if player:getMark("ma__lingdao1-turn") == 0 then
        table.insert(choices, "ma__lingdao1")
      end
      if player:getMark("ma__lingdao2-turn") == 0 then
        table.insert(choices, "ma__lingdao2")
      end
      if player:getMark("ma__lingdao3-turn") == 0 and
        table.find(room:getOtherPlayers(player, false), function (p)
          return not p:isNude()
        end) then
        table.insert(choices, "ma__lingdao3")
      end
      if #choices > 0 then
        event:setCostData(self, {extra_data = choices})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = event:getCostData(self).extra_data
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = ma__lingdao.name,
      prompt = "#ma__lingdao-choosing:::"..math.min(#player:getCardIds("he"),5),
      all_choices = {"ma__lingdao1", "ma__lingdao2", "ma__lingdao3", "Cancel"}
    })
    if choice ~= "Cancel" then
      if choice == "ma__lingdao1" then
        local choices = {}
        for i = 0, math.min(#player:getCardIds("he"),5), 1 do
          table.insert(choices, tostring(i))
        end
        table.insert(choices, "Cancel")
        local exchange = room:askToChoice(player, {
          choices = choices,
          skill_name = ma__lingdao.name,
          prompt = "#ma__lingdao-exchange:::"..math.min(#player:getCardIds("he"),5),
          cancelable = true,
        })
        if exchange ~= "Cancel" then
        event:setCostData(self, {exchange = tonumber(exchange), choice = choice})
        return true
        end
      elseif choice == "ma__lingdao3" then
        local targets = table.filter(room:getOtherPlayers(player, false), function (p)
          return not p:isNude()
        end)
        local to = room:askToChoosePlayers(player, {
          targets = targets,
          min_num = 1,
          max_num = 1,
          prompt = "#ma__lingdao-choose:::"..math.min(#player:getCardIds("he"),5),
          skill_name = ma__lingdao.name,
          cancelable = true,
        })
        if #to > 0 then
          event:setCostData(self, {tos = to, choice = choice})
          return true
        else
          return
        end
      else
        event:setCostData(self, {choice = choice})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    room:setPlayerMark(player, choice.."-turn", 1)
    if choice == "ma__lingdao1" then
      local cards = room:getNCards(event:getCostData(self).exchange)
      room:turnOverCardsFromDrawPile(player, cards, ma__lingdao.name)
      if #player:getCardIds("h") > 1 then
        local result = room:askToGuanxing(player, {
          cards = player:getCardIds("h"),
          bottom_limit = { 0, 0 },
          skill_name = ma__lingdao.name,
          skip = true,
        })
      room:swapCardsWithPile(player, result.top, cards, ma__lingdao.name, "Top")
      else
      room:swapCardsWithPile(player, player:getCardIds("h"), cards, ma__lingdao.name, "Top")
      end
    elseif choice == "ma__lingdao2" then
      local ids = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end, Player.HistoryTurn)
      ids = table.filter(ids, function (id)
        return table.contains(room.discard_pile, id)
      end)
      if #ids == 0 then return end
        if #ids > 0 then
          ids = room:askToChooseCards(player, {
            target = player,
            min = 0,
            max = math.min(#player:getCardIds("he"),5),
            flag = { card_data = {{ "pile_discard", ids }} },
            skill_name = ma__lingdao.name,
            prompt = "#ma__lingdao-discard_pile:::"..math.min(#player:getCardIds("he"),5),
          })
        end
      room:swapCardsWithPile(player, player:getCardIds("h"), ids, ma__lingdao.name, "discardPile", true, player)
    elseif choice == "ma__lingdao3" then
      local to = event:getCostData(self).tos[1]
      local cards = room:askToChooseCards(player, {
        target = to,
        min = 0,
        max = math.min(#player:getCardIds("he"),5),
        flag = "h",
        skill_name = ma__lingdao.name,
      })
      room:swapCards(player, {
        {player, player:getCardIds("h")},
        {to, cards},
      }, ma__lingdao.name)
    end
  end,
})

ma__lingdao:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "ma__lingdao1", 0)
  room:setPlayerMark(player, "ma__lingdao2", 0)
  room:setPlayerMark(player, "ma__lingdao3", 0)
end)

return ma__lingdao